<?php
namespace app\massage\model;

use app\BaseModel;
use think\facade\Db;

class GroupRatio extends BaseModel
{
    //定义表名
    protected $name = 'massage_service_group_ratio';

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){
        $data['create_time'] = time();
        $res = $this->insert($data);
        return $res;
    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){
        $res = $this->where($dis)->update($data);
        return $res;
    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$field = '*'){
        $data = $this->where($dis)
            ->field($field)
            ->order('id desc')
            ->select()
            ->toArray();
        return $data;
    }

    //身份列表
    public function list($dis,$page = 10,$field = '*'){
        $data = $this->where($dis)
            ->field($field)
            ->order('id asc')
            ->paginate($page)
            ->toArray();
        return $data;
    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){
        $data = $this->where($dis)->find();
        return !empty($data)?$data->toArray():[];
    }

    //设置分组个人比例
    public function setRatio($input){
        $group_id = $input['group_id'];
        $rule = $input['rule'] ?? [];
        $data = [];
        $time = time();
        foreach ($rule as $item) {
            $data[] = [
                'group_id' => $group_id,
                'type' => $item['type'],
                'user_id' => $item['user_id'],
                'ratio' => $item['ratio'],
                'create_time' => $time,
            ];
        }
        $where = [
            'group_id' => $group_id,
        ];
        $this->destroyRow($where);
        $res = $this->insertAll($data);
        return $res;
    }
}